var express = require('express');
const { update } = require('../model/filmAndCinemasModel');
var router = express.Router();
const filmAndCinemasServise = require('../service/filmAndCinemasService');

// findAll 不分页
router.get('/all', async function (require, resolve) {
    console.log('routes -- findAll()');

    let data = await filmAndCinemasServise.findAll(require.query);
    resolve.send(data)
})

// 根据日期，返回指定日期的 排片内容
// 传入 电影id 影院id 要查询的日期
router.get('/schedule/date', async function (require, resolve) {
    console.log('routes -- /schedule/date');
    let { date, filmId, cinemaId } = require.query

    let data = await filmAndCinemasServise.findByDate(date, filmId, cinemaId);
    resolve.send(data)
})

// find()
router.get('/', async function (require, resolve) {
    console.log('routes -- find()');

    let { type, value, current, size } = require.query;
    let searchObj = {};
    if (type && value) {
        searchObj[type] = value;
    }
    let data = await filmAndCinemasServise.find(searchObj, { current: current || 1, size: size || 3 });
    resolve.send(data);
})

// findById()
router.get('/:id', async function (require, resolve) {
    console.log('routes -- findById()');

    let id = require.params.id;
    let data = await filmAndCinemasServise.findById(id);
    resolve.send(data);
})

// 根据条件找 指定关联的 排片信息
router.get('/schedule/:id', async function (require, resolve) {
    console.log('routes -- schedule()');

    let { type, value, } = require.query;
    let id = require.params.id;
    let result = await filmAndCinemasServise.findById(id);
    if (type && value) {
        let arr = [];
        switch (type) {
            case 'date': {
                arr = result.schedule.filter(item => item.date == value)
                break;
            };
            case 'name': {
                arr = result.schedule.filter(item => item.name == value)
                break;
            };
            case 'time': {
                arr = result.schedule.filter(item => item.time == value)
                break;
            };
            case 'price': {
                arr = result.schedule.filter(item => item.price == value)
                break;
            };
        }
        result.schedule = arr
        console.log(result.schedule);
    }
    resolve.send(result);
})

// insert()
router.post('/', async function (require, resolve) {
    console.log('routes -- insert()');

    let obj = {
        cinema: require.body.cinema,
        film: require.body.film,
    }
    let isFind = await filmAndCinemasServise.findAll(obj);
    console.log(isFind);
    if (isFind.length > 0) {
        resolve.send('关联已存在');
    } else {
        console.log(obj);
        let data = await filmAndCinemasServise.insert(obj);
        resolve.send(data);
    }

})

// update()
router.put('/:id', async function (require, resolve) {
    console.log('routes -- update()');

    // console.log(JSON.parse(require.body.schedule));
    // cinema 和 film 不能为空
    let obj = {
        cinema: require.body.cinema,
        film: require.body.film,
        schedule: JSON.parse(require.body.schedule),
    };
    // console.log(obj);
    let id = require.params.id;
    let data = await filmAndCinemasServise.update(id, obj);
    resolve.send(data);
})

// update schedule 修改指定关联的 排片信息
router.put('/schedule/:id', async function (require, resolve) {
    console.log('routes -- updateSchedule()');

    let { oldScheduleJSON, newScheduleJSON } = require.body
    console.log(oldScheduleJSON, newScheduleJSON);

    let id = require.params.id;
    let data = await filmAndCinemasServise.updateSchedule(id, oldScheduleJSON, newScheduleJSON);
    resolve.send(data);
})

// delete()
router.delete('/:id', async function (require, resolve) {
    console.log('routes -- delete()');

    let id = require.params.id;
    let data = await filmAndCinemasServise.delete(id);
    resolve.send(data);
})

module.exports = router;